﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;
using BaseSet.Entity.Common.DataOperation;

namespace WebSite
{
    public partial class GetEmailOnLine : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region 单条上传
        protected void btnSubmitOne_Click(object sender, EventArgs e)
        {
            string email = txtEmail.Text.Trim();
            if (!string.IsNullOrEmpty(email))
            {
                if (!CheckEmail(email))
                {
                    CollectEmail ceinfo = new CollectEmail();
                    CollectEmailHelper cehelper = new CollectEmailHelper();
                    ceinfo.Email = email;
                    cehelper.AddCollectEmail(ceinfo);
                    Tools.AlertAndRedirect("添加成功，辛苦了", "GetEmailOnLine.aspx");
                    return;
                }
            }
        }
        #endregion

        #region 批量上传
        protected void btnSubmitBatch_Click(object sender, EventArgs e)
        {
            string serverPath = Server.MapPath("");
            string filePath = @serverPath + "\\Files\\Email";

            string fields = "Email";//要选择的字段
            string cueinfo = "";
            DataTable ds = getUploadData(FileUpload1, filePath, fields, ref cueinfo);

            if (ds != null && ds.Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Rows)
                {
                    string email = dr["email"] + "";
                    email = email.Trim();
                    if (!string.IsNullOrEmpty(email))
                    {
                        if (!CheckEmail(email))
                        {
                            CollectEmail ceinfo = new CollectEmail();
                            CollectEmailHelper cehelper = new CollectEmailHelper();
                            ceinfo.Email = email;
                            cehelper.AddCollectEmail(ceinfo);
                        }
                    }
                }
                Tools.AlertAndRedirect("上传成功，辛苦了", "GetEmailOnLine.aspx");
                return;
            }
            else
            {
                Tools.AlertAndRedirect("上传附件为空", "GetEmailOnLine.aspx");
                return;
            }
        }
        #endregion

        #region 判断重复
        private bool CheckEmail(string email)
        {
            //判断系统中是否已经存在
            CollectEmailHelper cehelper = new CollectEmailHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Email", email);
            int count = cehelper.QueryCollectEmailCountBySearch(c);
            if (count > 0)//已经存在
            {
                return true;
            }
            else//不存在
            {
                return false;
            }
        }
        #endregion

        protected string getUploadFilePath(FileUpload fuXls, string folderName, ref string cueInfo)
        {
            string path = "";
            if (fuXls.HasFile)
            {
                //取得文件扩展名
                string extension = System.IO.Path.GetExtension(fuXls.FileName);
                if (extension.ToLower() == ".xls")
                {
                    ////得到服务器根目录
                    //string serverPath = Server.MapPath("."); System
                    ////得到绝对路径(注意: 在asp.net里"\"必须用"\\"代替)
                    path = folderName + "\\" + System.Guid.NewGuid().ToString() + System.DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
                    //保存文件到服务器
                    try
                    {
                        fuXls.PostedFile.SaveAs(path);
                    }
                    catch
                    {
                        //保存出错
                        path = "";
                        cueInfo += "保存到服务器失败!!<br />";
                    }
                }
                else
                {
                    cueInfo += "请按照模板进行上传,后缀名为.xls!!<br />";
                }
            }
            else
            {
                cueInfo += "请选择要上传的Excel文件!!<br />";
            }
            return path;
        }

        protected DataTable getUploadData(FileUpload fuXls, string folderName, string fields, ref string cueInfo)
        {
            DataTable dtSell = null;
            //得到上传Xls文件在服务器上的绝对路径,为空("")上传失败  
            string path = getUploadFilePath(fuXls, folderName, ref cueInfo);
            if (!String.IsNullOrEmpty(path))
            {
                //得到服务器上的Xls填充的DataTable
                dtSell = FileOperation.GetDataListFromExcel(path, fields, ".xls", ref cueInfo);
            }

            return dtSell;
        }
    }
}